---
title: "Cleaning Cross-National Socio-Economic and Religion Data, 2011"
---

# load

```{r}
# load packages
  source("helper-packages.R")

# load RCS Dataset
  cnse_raw <- 
    import("../raw-data/x-cross-national-socio-economic-religion-data-2011/Cross-National Socio-Economic and Religion Data, 2011.xlsx")
```

# clean

```{r}
# clean
  cnse_clean <- 
    cnse_raw %>% 
    select(ICOUNTRY, ISO3, RELRECOD, CIANAME) %>% 
    mutate(
      cnse_largest_religion = 
        dplyr::recode(
          RELRECOD,
          "0" = "Atheism",
          "1" = "Christian",
          "2" = "Christian", #Catholic
          "3" = "Christian", #Orthodox Christian
          "4" = "Christian", #Protestant	
          "5" = "Christian", #Anglican/Episcopal
          "6" = "Muslim", #Muslim (unspecified)
          "7" = "Muslim", #Muslim Sunni	
          "8" = "Muslim", #Muslim Shi'a
          "10" = "Jewish",	
          "11" = "Animist/indigenous/traditional",	
          "12" = "Hindu", #Hindu (unspecified)	
          "14" = "Buddhist", #Buddhist (specified branch)	
          "15" = "Buddhist", #Buddhist (unspecified)	
          "17" = "Shinto",
          "19" = "Other religion")) %>%
    mutate(cnse_common_country_name = countryname(CIANAME)) %>% 
    filter(!is.na(cnse_largest_religion) & !is.na(cnse_common_country_name)) %>% 
    select(cnse_common_country_name, cnse_largest_religion)

# add additional countries to cnse list
  cnse_extras <- 
    tribble(
      ~cnse_common_country_name, ~cnse_largest_religion,
      "US", "Christian", # unsure why missing from cnse
      "Czechoslovakia", "Christian",
      "German Democratic Republic", "Christian",
      "Palestine", "Muslim")

# append
  cnse_stacked <- 
    cnse_clean %>% 
    bind_rows(cnse_extras) %>% 
    mutate(
      cnse_largest_religion = 
        case_when(
          cnse_common_country_name %in% c("Netherlands", "Czechia") ~ "Christian", # not athiest
          TRUE ~ cnse_largest_religion))
```

# Save data

```{r}
  saveRDS(cnse_stacked, "../cleaned-data/x-4-cnse-largest-relig.rds")
```
